//
//  userSignInController.m
//  toolBox
//
//  Created by Alex on 08/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "userSignInController.h"

@implementation userSignInController
@synthesize usernameField,passwordField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    [usernameField setDelegate:self];
    [self.usernameField setReturnKeyType:UIReturnKeyDone];
    [self.usernameField addTarget:self action:@selector(usernameField)forControlEvents:UIControlEventEditingDidEndOnExit];
    [passwordField setDelegate:self];
    [self.passwordField setReturnKeyType:UIReturnKeyDone];
    [self.passwordField addTarget:self action:@selector(passwordField)forControlEvents:UIControlEventEditingDidEndOnExit];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)signin:(id)sender
{
    NSLog(@"Started auth process");
    if(usernameField == nil || passwordField == nil)
    {
        UIAlertView *alertTemp = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Please complete all fields." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        
        [alertTemp show];
    }
    
    userAuthModel *tempRequest = [[userAuthModel alloc] isUserAuthed:usernameField.text
                                                            password:passwordField.text
                                                          withObject:self
                                                        withSelector:@selector(authReply:)];
    //fire off our auth request and chirp a response in the below method;
}

- (IBAction)openSheet:(id)sender 
{
    UIActionSheet *ac = [[UIActionSheet alloc] initWithTitle:@"Select" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Clear all data" otherButtonTitles:nil, nil];
    ac.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [ac showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
         NSLog(@"button zero");
        userAuthModel *deletedata = [[userAuthModel alloc] deleteDataBase:self withSelector:@selector(dbcallback:)];
        
			} else if (buttonIndex == 1) {
                
                NSLog(@"button one");
			} else if (buttonIndex == 2) {	
            } else if (buttonIndex == 3) {
	}
}
-(void)dbcallback:(id)sender
{
    NSLog(@"Deleted db");
    
}
-(void)authReply:(id)reply
{
    NSLog(@"chirp authReply"); 
    NSLog(@"%@",[reply isAuthed]);
    if([reply isAuthed] == @"NO")
    {
        UIAlertView *alerter = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"No username/password matching your input has been found." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alerter show];
        usernameField.text = nil;
        passwordField.text = nil;
        return;
    }
    if([reply isAuthed] == @"YES")
    {
        //proceed with login
        NSLog(@"Authentication successful"); 
    }
}
@end
